package com.secneo.appshare.activities;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.secneo.appshare.R;
import com.secneo.appshare.adapter.ImageAdapter;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.AppInfo;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.appshare.common.AppSharePreference;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.database.AppFile;
import com.secneo.appshare.database.AppShareDatabaseHelper;
import com.secneo.appshare.threads.GetAppInfoThread;
import com.secneo.appshare.views.IndicationDotList;
import com.secneo.appshare.views.MyGallery;
import com.secneo.mp.api.database.DatabaseHelper;

public class ApplicationInfoActivity extends Activity implements
		OnClickListener {

	private ImageAdapter mAdapter = null;
	private MyGallery mMyGallery = null;
	private IndicationDotList mDotList = null;

	private PopupWindow mImgPop = null;
	private View mImgView = null;
	private MyGallery mPopGallery = null;
	private IndicationDotList mPopIndicationDotList = null;
	private Button mPopDownloadBtn = null;
	private ImageAdapter mPopAdapter = null;

	private String package_name;
	private int number;
	private String uid;
	private Boolean installed_not_addicted;
	private Boolean user_app_flag;
	private String app_icon_url;
	private byte[] imageBytes;

	private AppInfo mAppInfo;

	private GetAppInfoThread mGetAppInfoThread;
	private ProgressDialog mWaitDlg;

	private TextView mAppNameTv = null;
	private ImageView mAppIconIv = null;
	private TextView mDownloadCountTv = null;
	private TextView mUserCountTv = null;
	private TextView mDesTv = null;

	private Button mCommentBtn = null;
	private Button mHideBtn = null;
	private Button mShareBtn = null;
	private Button mDownloadBtn = null;
	private Button mRunBtn = null;
	private Button mUninstallBtn = null;

	private boolean shareFlag = false;
	private boolean favorite_flag = false;
	private boolean isInstalled = false;
	
	private int popGalleryIndex = 0 ;	//pop弹出的是那张图片

	public static boolean mRecyleFlag = true;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constants.MODE_GET_ALL_APP_INFO:
				if (mWaitDlg != null && mWaitDlg.isShowing()) {
					mWaitDlg.dismiss();
				}
/*				
				
				((LinearLayout)findViewById(R.id.app_simple_detail))
							.setBackgroundColor(9) ;*/
				
				mAppInfo = mGetAppInfoThread.getAppInfo();

				if(!mAppInfo.appDetails.shortUrlList.isEmpty()){
					mMyGallery.setBackgroundResource(R.drawable.short_bg) ;
					mMyGallery.setVisibility(View.VISIBLE) ;
					((LinearLayout) findViewById(R.id.app_short_ll)).setVisibility(View.VISIBLE) ;
				}
				
				mAppNameTv.setVisibility(View.VISIBLE) ;
				mAppNameTv.setText(mAppInfo.appDetails.name);

				if(mAppInfo.appDetails.hotflag){
					ApplicationActivity.mHotcountButton.setBackgroundResource(R.drawable.hot_press) ;
					ApplicationActivity.mHotcountButton.setClickable(false) ;
				} else {
					ApplicationActivity.mHotcountButton.setBackgroundResource(R.drawable.hot) ;
					ApplicationActivity.mHotcountButton.setClickable(true) ;
				}
				ApplicationActivity.mHotcountTextView.setText(mAppInfo.appDetails.hotcount) ;
				
				
				mUserCountTv.setVisibility(View.VISIBLE) ;
				mUserCountTv.setText(mAppInfo.appDetails.userCount + "人正在使用");
				mDesTv.setVisibility(View.VISIBLE) ;
				if (mAppInfo.appDetails.description.equals("")) {
					mDesTv.setText("暂无关于" + mAppInfo.appDetails.name + "的信息。");
				} else {
					mDesTv.setText(mAppInfo.appDetails.description);
				}
				
				
				
				if (isInstalled) {
					mRunBtn.setVisibility(View.VISIBLE) ;
					mUninstallBtn.setVisibility(View.VISIBLE) ;
				} else {
					mDownloadBtn.setVisibility(View.VISIBLE) ;
					mDownloadCountTv.setVisibility(View.VISIBLE) ;
					mDownloadCountTv.setText(mAppInfo.appDetails.downloadCount
							+ "次下载");
				}
				mCommentBtn.setVisibility(View.VISIBLE);
				mShareBtn.setVisibility(View.VISIBLE);
				mHideBtn.setVisibility(View.VISIBLE);

				break;
			case Constants.MSG_GET_APP_INFO_NETWORK_ERROR:
				if (mWaitDlg != null && mWaitDlg.isShowing()) {
					mWaitDlg.dismiss();
				}
				showNetworkErrView();
				break;
			case Constants.MSG_GET_APP_INFO_ICON_FINISH:
				mAppIconIv.setVisibility(View.VISIBLE) ;
				mAppIconIv
						.setBackgroundDrawable(mAppInfo.appDetails.iconDrawable);
				break;
			case Constants.MSG_GET_APP_INFO_SHORT_FINISH:
				mAdapter.addImg(mAppInfo.appDetails.shortDrawableList
						.get(msg.arg1));
				mDotList.setCount(mDotList.getCount() + 1);
				break;
			case Constants.MSG_GET_APP_INFO_STATUS_ERROR:
				break;
			case Constants.MSG_GET_APP_INFO_STATUS_OK:
				break;
			case Constants.MSG_ACTION_SHARE_OK:	//share ok
				ActivityUtil.showToast(ApplicationInfoActivity.this, 
						ApplicationInfoActivity.this.getResources().
							getText(R.string.share_success_title).toString(), 
						ApplicationInfoActivity.this.getResources()
							.getText(R.string.share_success_content).toString());
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appshare_app_info);

		parseIntent();

		mAppNameTv = (TextView) findViewById(R.id.app_name_tv);
		mDownloadCountTv = (TextView) findViewById(R.id.app_download_count_tv);
		mUserCountTv = (TextView) findViewById(R.id.app_user_count_tv);
		mDesTv = (TextView) findViewById(R.id.app_txt_detail_tv);
		mAppIconIv = (ImageView) findViewById(R.id.app_icon_iv);

		mCommentBtn = (Button) findViewById(R.id.button_app_comment);
		mCommentBtn.setOnClickListener(CommentClickListener);
		mHideBtn = (Button) findViewById(R.id.button_app_hidden);
		mHideBtn.setOnClickListener(hiddenClickListener);
		mShareBtn = (Button) findViewById(R.id.app_share_btn);
		mShareBtn.setOnClickListener(ShareClickListener);
		mDownloadBtn = (Button) findViewById(R.id.btn_download);
		mDownloadBtn.setOnClickListener(DownloadClickListener);
		mRunBtn = (Button) findViewById(R.id.btn_run);
		mRunBtn.setOnClickListener(OpenAppListener);
		mUninstallBtn = (Button) findViewById(R.id.btn_uninstall);
		mUninstallBtn.setOnClickListener(uninstallClickListener);

		Gallery.LayoutParams params = new Gallery.LayoutParams(250, 330);
		mAdapter = new ImageAdapter(this, params, ImageView.ScaleType.FIT_XY);

		mMyGallery = (MyGallery) findViewById(R.id.app_short_gallery);
		mMyGallery.setOnItemClickListener(galleryClickListener);
		mMyGallery.setOnItemSelectedListener(gallerySelectListener);
		mMyGallery.setAdapter(mAdapter);

		mMyGallery.setSpacing(30);

		mDotList = (IndicationDotList) findViewById(R.id.dot_list);

		mGetAppInfoThread = new GetAppInfoThread(getApplicationContext(),
				mHandler, package_name, number, uid, installed_not_addicted);
		mGetAppInfoThread.start();

		showDialog();
	}

	private void rececyle(){
		if (mRecyleFlag) {
			// 回收资源
			mRecyleFlag = false;
			mMyGallery.setAdapter(null);
			if (mAdapter != null)
				mAdapter.freeDrawable();
			mDotList.setCount(0);

			if (mPopGallery != null)
				mPopGallery.setAdapter(null);
			if (mPopAdapter != null)
				mPopAdapter.freeDrawable();
			if (mPopIndicationDotList != null)
				mPopIndicationDotList.setCount(0);
			
			
			{
				mMyGallery.setVisibility(View.GONE) ;
				mAppNameTv.setVisibility(View.GONE) ;
				mAppIconIv.setVisibility(View.GONE) ;
				mDownloadBtn.setVisibility(View.GONE) ;
				mDownloadCountTv.setVisibility(View.GONE) ;
				mShareBtn.setVisibility(View.GONE) ;
				mCommentBtn.setVisibility(View.GONE) ;
				mHideBtn.setVisibility(View.GONE) ;
				mRunBtn.setVisibility(View.GONE) ;
				mUninstallBtn.setVisibility(View.GONE) ;
				mUserCountTv.setVisibility(View.GONE) ;
				mDesTv.setVisibility(View.GONE) ;
			}
			
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		rececyle() ;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mRecyleFlag = false;
		mMyGallery.setAdapter(null);
		if (mAdapter != null)
			mAdapter.freeDrawable();
		mDotList.setCount(0);

		if (mPopGallery != null)
			mPopGallery.setAdapter(null);
		if (mPopAdapter != null)
			mPopAdapter.freeDrawable();
		if (mPopIndicationDotList != null)
			mPopIndicationDotList.setCount(0);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		Boolean needInit = intent.getBooleanExtra("FromGroup", false);
		if (needInit) {
			mGetAppInfoThread = new GetAppInfoThread(getApplicationContext(),
					mHandler, package_name, number, uid, installed_not_addicted);
			mGetAppInfoThread.start();

			Gallery.LayoutParams params = new Gallery.LayoutParams(250, 330);
			mAdapter = new ImageAdapter(this, params,
					ImageView.ScaleType.FIT_XY);
			mMyGallery.setVisibility(View.GONE) ;
			mMyGallery.setAdapter(mAdapter);

			if (mDotList != null) {
				mDotList.setCount(0);
			}

			showDialog();
		}
		super.onNewIntent(intent);
	}

	private void showNetworkErrView() {
		View view = ApplicationInfoActivity.this.findViewById(R.id.error_user);
		view.setVisibility(View.VISIBLE);
	}

	private void showDialog() {

		View view = ApplicationInfoActivity.this.findViewById(R.id.error_user);
		view.setVisibility(View.GONE);

		mWaitDlg = new ProgressDialog(this);
		String msg = getResources().getString(R.string.txt_loading);
		mWaitDlg.setMessage(msg);
		mWaitDlg.setCancelable(true);
		mWaitDlg.show();
	}

	private void parseIntent() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			package_name = bundle.getString("package_name");
			number = bundle.getInt("n", 20);
			uid = bundle.getString("uid");
			installed_not_addicted = bundle.getBoolean(
					"installed_not_addicted", true);
			user_app_flag = bundle.getBoolean("user_app_flag");
			app_icon_url = bundle.getString("app_icon");
			imageBytes = bundle.getByteArray("app_icon_array");
			Log.d("TAG", "user_app_flag = " + user_app_flag
					+ "installed_app_addicated = " + installed_not_addicted);
		}

		if (package_name != null && !package_name.equals("")) {
			PackageManager manager = getPackageManager();
			isInstalled = false;
			try {
				PackageInfo info = manager.getPackageInfo(package_name, 0);
				if (info != null) {
					isInstalled = true;
				} else
					isInstalled = false;

			} catch (Exception e) {
				isInstalled = false;
			}
		}
	}

	private void initImagePopWindow() {
		mImgView = getLayoutInflater().inflate(R.layout.appshare_app_pop, null);
		mImgPop = new PopupWindow(mImgView, LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT, true);
		
		mPopDownloadBtn = (Button) mImgView.findViewById(R.id.pop_download_btn);
		mPopDownloadBtn.setOnClickListener(DownloadClickListener);

		RelativeLayout popRl = (RelativeLayout)mImgView.findViewById(R.id.app_pop_rl) ;
		popRl.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mImgPop != null && mImgPop.isShowing()){
					mImgPop.dismiss() ;
				}
			}
		}) ;
		
		mImgPop.setAnimationStyle(R.style.AnimationPreview);

		Gallery.LayoutParams params = new Gallery.LayoutParams(410, 520
		/*
		 * ViewGroup.LayoutParams.FILL_PARENT,
		 * ViewGroup.LayoutParams.FILL_PARENT
		 */);
		mPopAdapter = new ImageAdapter(this, params, ImageView.ScaleType.FIT_XY);

		mPopGallery = (MyGallery) mImgView.findViewById(R.id.pop_img_gallery);

		mPopGallery.setSpacing(10);
		mPopGallery.setAdapter(mPopAdapter);
		mPopGallery.setOnItemClickListener(popGalleryClickListener);
		mPopGallery.setOnItemSelectedListener(popGallerySelectListener);
		mPopGallery.setSelection(popGalleryIndex) ;

		for (int i = 0; i < mAppInfo.appDetails.shortDrawableList.size(); ++i) {
			mPopAdapter.addImg(mAppInfo.appDetails.shortDrawableList.get(i));
		}

		RelativeLayout vv = (RelativeLayout) findViewById(R.id.app_info_all_rl);
		vv.setOnClickListener(this);
		// 加上下面两行可以用back键关闭popupwindow，否则必须调用dismiss();
		ColorDrawable dw = new ColorDrawable(-00000);
		mImgPop.setBackgroundDrawable(dw);
		mImgPop.showAtLocation(vv, Gravity.CENTER, 0, 0);

//		mImgPop.update(0, 0, 360, 550);
		mImgPop.update() ;

		mPopIndicationDotList = (IndicationDotList) mImgView
				.findViewById(R.id.pop_dot_list);
		mPopIndicationDotList.setCount(3);
		/*
		 * mPopCloseBtn = (Button) mImgView.findViewById(R.id.close_btn);
		 * mPopCloseBtn.setOnClickListener(closeListener);
		 */

		mMyGallery.invalidate();
	}

	OnClickListener closeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (mImgPop != null && mImgPop.isShowing()) {
				mImgPop.dismiss();
			}
		}
	};

	/*
	 * 当用户滑动了图片回调
	 */
	private final OnItemSelectedListener gallerySelectListener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			mDotList.setIndex(position);
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {

		}
	};

	/*
	 * 当用户点击了gallery上的图片回调
	 * 
	 * @selectItemIndex
	 */
	private final OnItemClickListener galleryClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			popGalleryIndex = position ;
			initImagePopWindow();
		}
	};

	/*
	 * 当用户滑动了图片回调
	 */
	private final OnItemSelectedListener popGallerySelectListener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			mPopIndicationDotList.setIndex(position);
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {

		}
	};

	/*
	 * 当用户点击了gallery上的图片回调
	 * 
	 * @selectItemIndex
	 */
	private final OnItemClickListener popGalleryClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

		}
	};

	@Override
	public void onClick(View v) {
		if (mImgPop != null && mImgPop.isShowing()) {
			mImgPop.dismiss();
		}
	}

	// ping lun
	OnClickListener CommentClickListener = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if(!AppSharePreference.isAppShareUserLogined(ApplicationInfoActivity.this))
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationInfoActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
			}else
			{
				DatabaseHelper dh = new DatabaseHelper(ApplicationInfoActivity.this);
				dh.insertUserAppPopularityBycatagory(ApplicationInfoActivity.this,
						2020);
				dh.close();
				showRemak();
			}
		}
	};

	public void showRemak() {
		PopAlarmActivity
				.show(ApplicationInfoActivity.this, package_name,
						mGetAppInfoThread.getAppInfo().appDetails.name, 0,
						app_icon_url);
	}

	// share
	OnClickListener ShareClickListener = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if(!AppSharePreference.isAppShareUserLogined(ApplicationInfoActivity.this))
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationInfoActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
			}else
			{
				try {
					Thread shareThread = new Thread() {
						public void run() {
							// 0:bangbang; 1:sina; 2:tencent; 3:renren: 4:kaixin
							LocalStore localStroe = LocalStore
									.getLocaleStoreInstance(ApplicationInfoActivity.this);
							RemoteApiImpl remoteApi = new RemoteApiImpl();
							if (shareFlag) {
								Message msg = new Message();
								msg.what = Constants.MSG_ACTION_MSG_DROP;
								mHandler.sendMessage(msg);
								return;
							} else {
								shareFlag = true;
								remoteApi.sendShareAction(
										ApplicationInfoActivity.this, package_name);
								String appDes = mGetAppInfoThread.getAppInfo().appDetails.description;
								String goDes = "";
								if (appDes != null)
									if (appDes.length() > 50)
										goDes = appDes.substring(0, 50) + "......";
									else
										goDes = appDes;
								if (localStroe.accountIsActive(1)
										|| localStroe.accountIsActive(3)) {
									// 新浪微博分享
									ActivityUtil
											.shareToWeibo(
													ApplicationInfoActivity.this,
													Constants.UPLOAD_ACTION_SHARE_WEIBO,
													mGetAppInfoThread.getAppInfo().appDetails.name,
													"",
													imageBytes,
													null,
													app_icon_url,
													goDes,
													mGetAppInfoThread.getAppInfo().appDetails.package_name);
	
								}
	
								if (localStroe.accountIsActive(2)) {
									// 腾讯微博分享
									if (!Constants.SWITCH_SINA_CUSTOM) {
										if (imageBytes == null) {
											remoteApi
													.share2QWeiboMsg(
															ApplicationInfoActivity.this,
															Constants.UPLOAD_ACTION_SHARE_WEIBO,
															mGetAppInfoThread
																	.getAppInfo().appDetails.name,
															null,
															goDes,
															mGetAppInfoThread
																	.getAppInfo().appDetails.package_name);
										} else {
											remoteApi
													.share2QWeiboMsg(
															ApplicationInfoActivity.this,
															Constants.UPLOAD_ACTION_SHARE_WEIBO,
															mGetAppInfoThread
																	.getAppInfo().appDetails.name,
															imageBytes,
															null,
															goDes,
															mGetAppInfoThread
																	.getAppInfo().appDetails.package_name);
										}
									}
								}
	
								Message msg = new Message();
								msg.what = Constants.MSG_ACTION_SHARE_OK;
								mHandler.sendMessage(msg);
							}
						}
					};
					shareThread.start();
				} catch (Exception e) {
	
					try {
						Toast.makeText(ApplicationInfoActivity.this,
								R.string.share_error, 2000).show();
					} catch (Exception ex) {
					}
					e.printStackTrace();
				}
			}
		}
	};

	// hidden
	OnClickListener hiddenClickListener = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if(!AppSharePreference.isAppShareUserLogined(ApplicationInfoActivity.this))
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationInfoActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
			}else
			{
				DatabaseHelper dh = new DatabaseHelper(ApplicationInfoActivity.this);
				dh.insertUserAppPopularityBycatagory(ApplicationInfoActivity.this,
						2018);
				dh.close();
				try {
					Thread sendFavoriteThread = new Thread() {
						public void run() {
							RemoteApiImpl rai = new RemoteApiImpl();
							String uid = LocalStore.getLocaleStoreInstance(
									ApplicationInfoActivity.this).getUid();
							JSONObject obj = rai.getUserAction(
									ApplicationInfoActivity.this, uid, "",
									package_name, "7");
							try {
								if (!"".equals(obj) && obj != null) {
									if ("ok".equals(obj.getString("status"))) {
										if (favorite_flag) {
											AppShareDatabaseHelper adh = new AppShareDatabaseHelper(
													ApplicationInfoActivity.this);
											AppFile appFile = adh
													.getAppFileByPackageName(adh,
															package_name);
											appFile.setHidden(0);
											appFile.setUpdateServer(1);
											adh.updateAppFile(appFile);
										} else {
											AppShareDatabaseHelper adh = new AppShareDatabaseHelper(
													ApplicationInfoActivity.this);
											AppFile appFile = adh
													.getAppFileByPackageName(adh,
															package_name);
											appFile.setHidden(1);
											appFile.setUpdateServer(1);
											adh.updateAppFile(appFile);
										}
										Message msg = new Message();
										msg.what = Constants.MSG_ACTION_FAVORITE_OK;
										mHandler.sendMessage(msg);
									} else {
										Message msg = new Message();
										msg.what = Constants.MSG_ACTION_FAVORITE_ERROR;
										mHandler.sendMessage(msg);
									}
								} else {
									Message msg = new Message();
									msg.what = Constants.MSG_ACTION_FAVORITE_ERROR;
									mHandler.sendMessage(msg);
								}
	
							} catch (JSONException e) { // TODO Auto-generated catch
								// block
								e.printStackTrace();
							}
	
							catch (Exception e) { // TODO Auto-generated catch block
								e.printStackTrace();
							}
	
						}
					};
					sendFavoriteThread.start();
	
				} catch (Exception e) { // TODO Auto-generated catch block
					Toast.makeText(ApplicationInfoActivity.this,
							R.string.favorite_error, 2000).show();
					e.printStackTrace();
				}
			}
		}
	};

	OnClickListener DownloadClickListener = new OnClickListener() {
		public void onClick(View v) {
			DatabaseHelper dh = new DatabaseHelper(ApplicationInfoActivity.this);
			dh.insertUserAppPopularityBycatagory(ApplicationInfoActivity.this,
					2033);
			dh.close();
			AppInfo app = mGetAppInfoThread.getAppInfo();
			String appDes = app.appDetails.description;
			String goDes = "";
			if (appDes.length() > 50)
				goDes = appDes.substring(0, 50) + "......";
			else
				goDes = appDes;
			DownloadActivity.beginDownload(ApplicationInfoActivity.this,
					package_name, app.appDetails.name,
					app.appDetails.app_version, app.appDetails.app_size,
					app.appDetails.download_url, imageBytes, app_icon_url,
					goDes);
		}
	};

	OnClickListener uninstallClickListener = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if(!AppSharePreference.isAppShareUserLogined(ApplicationInfoActivity.this))
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationInfoActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
			}else
			{
				DatabaseHelper dh = new DatabaseHelper(ApplicationInfoActivity.this);
				dh.insertUserAppPopularityBycatagory(ApplicationInfoActivity.this,
						2019);
				dh.close();
				try {
					ActivityUtil.uninstallApp(ApplicationInfoActivity.this,
							package_name);
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(ApplicationInfoActivity.this,
							R.string.app_start_error, 2000).show();
				}
			}
		}
	};

	OnClickListener OpenAppListener = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if(!AppSharePreference.isAppShareUserLogined(ApplicationInfoActivity.this))
			{
				Intent intent=new Intent();
				intent.setClass(ApplicationInfoActivity.this, LoginChooseMethodActivity.class);
				startActivity(intent);
			}else
			{
				DatabaseHelper dh = new DatabaseHelper(ApplicationInfoActivity.this);
				dh.insertUserAppPopularityBycatagory(ApplicationInfoActivity.this,
						2021);
				dh.close();
				PackageManager manager = ApplicationInfoActivity.this
						.getPackageManager();
				PackageInfo info;
				try {
					info = manager.getPackageInfo(package_name, 0);
					Intent it = new Intent("android.intent.action.MAIN"); //$NON-NLS-1$
					it.addCategory(Intent.CATEGORY_LAUNCHER);
					List<ResolveInfo> acts = getPackageManager()
							.queryIntentActivities(it, 0);
		
					if (acts != null) {
		
						for (int i = 0, size = acts.size(); i < size; i++) {
							ResolveInfo ri = acts.get(i);
		
							if (package_name.equals(ri.activityInfo.packageName)) {
								it.setClassName(ri.activityInfo.packageName,
										ri.activityInfo.name);
		
								it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
										.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		
								startActivity(it);
		
								break;
							}
						}
					}
				} catch (NameNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(ApplicationInfoActivity.this,
							R.string.app_start_error, 2000).show();
				}
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && mImgPop != null
				&& mImgPop.isShowing()) {
			mImgPop.dismiss();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
